package com.acme.bankapp.domain.client;

import com.acme.bankapp.domain.account.AbstractAccount;
import com.acme.bankapp.domain.account.CheckingAccount;
import com.acme.bankapp.domain.account.SavingAccount;
import com.acme.bankapp.exceptions.NotEnoughFundsException;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

public class Client implements Serializable {

    private String name;
    private Gender gender;
    private List<AbstractAccount> accounts = new LinkedList<>();

    public Client(String name, Gender gender, double amount, double overdraft) throws IllegalArgumentException {
        this.name = name;
        this.gender = gender;
        addAccount(amount, overdraft);
    }

    public String getName() {
        return name;
    }

    public double getBalance() {
        double sum = 0;
        for (AbstractAccount am : accounts) {
            sum += am.getBalance();
        }
        return sum;
    }

    public void modifyAccount(double transactionAmount) throws NotEnoughFundsException {
        if (transactionAmount > 0) {
            getAccountFromSet().deposit(transactionAmount);
        } else {
            getAccountFromSet().withdraw(Math.abs(transactionAmount));
        }
    }

    public void addAccount(double amount, double overdraft) throws IllegalArgumentException {
        if (overdraft > 0)
            accounts.add(new CheckingAccount(amount, overdraft));
        else
            accounts.add(new SavingAccount(amount));
    }

    public void getClientSalutation() {
        System.out.println(gender.getSalutation() + " " + name);
    }

    public void printMaximumAccountToWithDraw() {
        for (AbstractAccount account : accounts) {
            System.out.println("BalanceWithDraw: " + account.maximumAmountWithDraw());
        }
    }

    private AbstractAccount getAccountFromSet() {
        for (AbstractAccount account : accounts) {
            return account;
        }
        return null;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (AbstractAccount account : accounts) {
            sb.append("accounttype=");
            sb.append(account.toString());
        }
        sb.append("name=").append(name).append(';');
        sb.append("gender=").append(gender).append(";");

        return sb.toString();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Client)) return false;

        Client client = (Client) o;

        if (gender != client.gender) return false;
        return !(name != null ? !name.equals(client.name) : client.name != null);
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + (gender != null ? gender.hashCode() : 0);
        return result;
    }
}
